package com.zhanghang.vending_machine.controller;

import cn.hutool.core.collection.CollUtil;
import com.zhanghang.vending_machine.common.result.Result;
import com.zhanghang.vending_machine.domain.dto.ShoppingCartDto;
import com.zhanghang.vending_machine.domain.po.ShoppingCart;
import com.zhanghang.vending_machine.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
@RequiredArgsConstructor
@Api(tags = "购物车管理接口")
@CrossOrigin(origins = "*")
public class ShoppingCartController {

    private final ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result add(@RequestBody ShoppingCartDto shoppingCartDto){
        log.info("添加购物车：{}",shoppingCartDto);

        shoppingCartService.addShoppingCart(shoppingCartDto.getGoodId(),shoppingCartDto.getNumber());

        return Result.success();
    }


    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> shoppingCartList(){
        log.info("查看购物车");

        List<ShoppingCart> list = shoppingCartService.list();

        return Result.success(list);
    }

    @PostMapping("/sub/{goodId}")
    @ApiOperation("删除一个商品")
    public Result sub(@PathVariable Long goodId){
        log.info("删除id为：{}的单条商品",goodId);

        shoppingCartService.subByGoodId(goodId);

        return Result.success();
    }

    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result subAll(){
        shoppingCartService.sub();

        return Result.success();
    }
}
